package com.chensi.handler;


/*import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;*/

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AopLogger 
{
	private static final Logger log = Logger.getLogger(AopLogger.class);

	/**
	 * 未捕捉的异常拦截
	 * @param joinPoint
	 * @param ex
	 * @throws Exception
	 */
	@AfterThrowing(pointcut = "execution(* com.chensi.controller.*.*(..))", throwing = "ex")
	public void loggerException(JoinPoint joinPoint, Exception ex) throws Exception
	{
		log.error("=========  发现未捕捉的异常  =========");
		log.error("异常名:" + ex.toString());
		String clazzName = joinPoint.getSignature().getDeclaringTypeName();
		log.error("类名:" + clazzName);
		String methodName = joinPoint.getSignature().getName();
		log.error("方法名:" + methodName);

		//String[] paramNames = getParamName(clazzName, methodName);

		/*Object object[] = joinPoint.getArgs();
		StringBuffer sb=new StringBuffer();
		if (object != null && object.length > 0)
		{
			for (int i = 0; i < object.length; i++)
			{
				//log.error(" 参数" + paramNames[i] + ":" + object[i]);
				sb.append("参数" + (i+1) + ":" + object[i]+",");
			}
		}
		log.error(sb.deleteCharAt(sb.lastIndexOf(",")));*/
	}
	
	/**
	 * 环绕日志
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	/*@Around("execution(* com.chensi.*.*.*(..))")
	public Object loggerAround(ProceedingJoinPoint joinPoint) throws Throwable{
		log.info("==> "+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
		Object o= joinPoint.proceed();
		//log.info("leave "+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
		return o;
		
	}*/
	
	
	
	/**
	 * 使用javaassist的反射方法获取方法的参数名
	 * @param clazzName
	 * @param methodName
	 * @return
	 * @throws NotFoundException
	 */
	/*private String[] getParamName(String clazzName, String methodName) throws NotFoundException
	{
		ClassPool pool = ClassPool.getDefault();
		CtClass cc = pool.get(clazzName);
		CtMethod cm = cc.getDeclaredMethod(methodName);
		MethodInfo methodInfo = cm.getMethodInfo();
		CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
		LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
		if (attr == null)
		{
			log.error("参数为空");
			return new String[0];
		}
		String[] paramNames = new String[cm.getParameterTypes().length];
		int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
		for (int i = 0; i < paramNames.length; i++)
		{
			paramNames[i] = attr.variableName(i + pos);
		}
		return paramNames;
	}*/
}
